//
// Copyright 2015 KISS Technologies GmbH, Switzerland
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#include <cstring>

#include "cpp-lib/blowfish.h"

#include "test_key.h"


using namespace cpl::crypt;


template< typename C >
unsigned number( C c ) 
{ return static_cast< unsigned >( c ) & 0xff ; }


template< typename C >
void hex_block( std::vector< C > const& v ) {

  std::cout << std::hex ;

  for( unsigned long i = 0 ; i < v.size() ; ++i ) 
  { std::cout << number( v[ i ] ) << ' ' ; }

  std::cout << '\n' ;

}


template< typename C > void test_char_block() {

  blowfish bf( key() ) ;

  std::cout << "Enter lines or END:\n" ;
  std::string s ;
  while( std::getline( std::cin , s ) ) {

	if( "END" == s ) { return ; }

	std::vector< C > v( bf.block_size() ) ;

	std::copy( 
	  s.begin() , 
	  s.begin() + std::min( static_cast< std::size_t >( 8 ) , v.size() ) ,
	  v.begin() 
	) ;

	hex_block( v ) ;
	bf.encrypt_block( v ) ;
	hex_block( v ) ;
	bf.decrypt_block( v ) ;
	hex_block( v ) ;

  }

}


int main()
{
	//TESTING Blowfish
	//ecb test data (taken from the DES validation tests)
	char szData[34][3][33] = {
		{"0000000000000000", "0000000000000000", "A5D0D080D945C4E44EF997456198DD78"},
		{"FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF", "441D9AF888D9E2BA51866FD5B85ECB8A"},
		{"3000000000000000", "1000000000000001", "A64CF55CC618BEEB7D856F9A613063F2"},
		{"1111111111111111", "1111111111111111", "94FFE4986D7AF8152466DD878B963C9D"},
		{"0123456789ABCDEF", "1111111111111111", "9EAEB9A14496B5BF61F9C3802281B096"},
		{"1111111111111111", "0123456789ABCDEF", "94FFE4986D7AF8157D0CC630AFDA1EC7"},
		{"0000000000000000", "0000000000000000", "A5D0D080D945C4E44EF997456198DD78"},
		{"FEDCBA9876543210", "0123456789ABCDEF", "64609F170CD672BF0ACEAB0FC6A0A28D"},
		{"7CA110454A1A6E57", "01A1D6D039776742", "794DD224E2608F0E59C68245EB05282B"},
		{"0131D9619DC1376E", "5CD54CA83DEF57DA", "001CB217394A4AEEB1B8CC0B250F09A0"},
		{"07A1133E4A0B2686", "0248D43806F67172", "8E90A0B63796BCFE1730E5778BEA1DA4"},
		{"3849674C2602319E", "51454B582DDF440A", "0E2D9D9104758072A25E7856CF2651EB"},
		{"04B915BA43FEB5B6", "42FD443059577FA2", "6B6F5557DC545F8B353882B109CE8F1A"},
		{"0113B970FD34F2CE", "059B5E0851CF143A", "7CE0B95D6409A31B48F4D0884C379918"},
		{"0170F175468FB5E6", "0756D8E0774761D2", "7B1AF57272E7C727432193B78951FC98"},
		{"43297FAD38E373FE", "762514B829BF486A", "2EB939302AF20A3F13F04154D69D1AE5"},
		{"07A7137045DA2A16", "3BDD119049372802", "0B531A354FE4D3DC2EEDDA93FFD39C79"},
		{"04689104C2FD3B2F", "26955F6835AF609A", "525B077ED4A1812CD887E0393C2DA6E3"},
		{"37D06BB516CB7546", "164D5E404F275232", "FE82D688D15621FC5F99D04F5B163969"},
		{"1F08260D1AC2465E", "6B056E18759F5CCA", "DF5A6BD4F3C5B7E24A057A3B24D3977B"},
		{"584023641ABA6176", "004BD6EF09176062", "517A829C986B748F452031C1E4FADA8E"},
		{"025816164629B007", "480D39006EE762F2", "10BAE61B5ED307EE7555AE39F59B87BD"},
		{"49793EBC79B3258F", "437540C8698F3CFA", "70998B4B05D6775E53C55F9CB49FC019"},
		{"4FB05E1515AB73A7", "072D43A077075292", "FA83CA46DBE5B94B7A8E7BFA937E89A3"},
		{"49E95D6D4CA229BF", "02FE55778117F12A", "13AB912B7A336622CF9C5D7A4986ADB5"},
		{"018310DC409B26D6", "1D9D5C5018F728C2", "AFB49C2BF65BB7D0D1ABB290658BC778"},
		{"1C587F1C13924FEF", "305532286D6F295A", "96102419A06E717555CB3774D13EF201"},
		{"0101010101010101", "0123456789ABCDEF", "40FAC9D4147B6636FA34EC4847B268B2"},
		{"1F1F1F1F0E0E0E0E", "0123456789ABCDEF", "AECAE1BBF878E283A790795108EA3CAE"},
		{"E0FEE0FEF1FEF1FE", "0123456789ABCDEF", "5403D695B6B0CFE6C39E072D9FAC631D"},
		{"0000000000000000", "FFFFFFFFFFFFFFFF", "A5D0D080D945C4E4014933E0CDAFF6E4"},
		{"FFFFFFFFFFFFFFFF", "0000000000000000", "441D9AF888D9E2BAF21E9A77B71C49BC"},
		{"0123456789ABCDEF", "0000000000000000", "9EAEB9A14496B5BF245946885754369A"},
		{"FEDCBA9876543210", "FFFFFFFFFFFFFFFF", "64609F170CD672BF6B5C5A9C5D9E0A5A"}
	};

  buffer aucKey;

  std::string tmp("test 1 2 3 4 5 6 7 8 9 0");
  buffer szChar(tmp.begin(), tmp.end());

  std::string cipher;
  std::string decipher;
  std::string szHex = blowfish::char2Hex(szChar);

  std::cout << "Key              Data             Ciphered                         Deciphered" << std::endl;
  std::cout << "===================================================================================" << std::endl;

	for(int i=0; i<34; i++)
	{
    szHex = szData[i][0];
    std::cout << szHex << " ";

    aucKey = blowfish::hex2Char(szHex);
    blowfish oBlowFish(aucKey);

    szHex = szData[i][1];
		std::cout << szHex << " ";
    szChar = blowfish::hex2Char(szHex);
 
    oBlowFish.encrypt(szChar);

    cipher = blowfish::char2Hex(szChar);

    std::cout << cipher << " ";

    if (std::strcmp(cipher.c_str(), szData[i][2]) != 0)
      std::cout << "ERROR1 ";

    oBlowFish.decrypt(szChar);

    decipher = blowfish::char2Hex(szChar);

    std::cout << decipher << " ";

    if (szHex != decipher)
      std::cout << "ERROR2 ";

    std::cout << std::endl;

	}

/*
//OK, the same results
Key              Text             Cipher
0000000000000000 0000000000000000 A5D0D080D945C4E44EF997456198DD78
FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 441D9AF888D9E2BA51866FD5B85ECB8A
3000000000000000 1000000000000001 A64CF55CC618BEEB7D856F9A613063F2
1111111111111111 1111111111111111 94FFE4986D7AF8152466DD878B963C9D
0123456789ABCDEF 1111111111111111 9EAEB9A14496B5BF61F9C3802281B096
1111111111111111 0123456789ABCDEF 94FFE4986D7AF8157D0CC630AFDA1EC7
0000000000000000 0000000000000000 A5D0D080D945C4E44EF997456198DD78
FEDCBA9876543210 0123456789ABCDEF 64609F170CD672BF0ACEAB0FC6A0A28D
7CA110454A1A6E57 01A1D6D039776742 794DD224E2608F0E59C68245EB05282B
0131D9619DC1376E 5CD54CA83DEF57DA 001CB217394A4AEEB1B8CC0B250F09A0
07A1133E4A0B2686 0248D43806F67172 8E90A0B63796BCFE1730E5778BEA1DA4
3849674C2602319E 51454B582DDF440A 0E2D9D9104758072A25E7856CF2651EB
04B915BA43FEB5B6 42FD443059577FA2 6B6F5557DC545F8B353882B109CE8F1A
0113B970FD34F2CE 059B5E0851CF143A 7CE0B95D6409A31B48F4D0884C379918
0170F175468FB5E6 0756D8E0774761D2 7B1AF57272E7C727432193B78951FC98
43297FAD38E373FE 762514B829BF486A 2EB939302AF20A3F13F04154D69D1AE5
07A7137045DA2A16 3BDD119049372802 0B531A354FE4D3DC2EEDDA93FFD39C79
04689104C2FD3B2F 26955F6835AF609A 525B077ED4A1812CD887E0393C2DA6E3
37D06BB516CB7546 164D5E404F275232 FE82D688D15621FC5F99D04F5B163969
1F08260D1AC2465E 6B056E18759F5CCA DF5A6BD4F3C5B7E24A057A3B24D3977B
584023641ABA6176 004BD6EF09176062 517A829C986B748F452031C1E4FADA8E
025816164629B007 480D39006EE762F2 10BAE61B5ED307EE7555AE39F59B87BD
49793EBC79B3258F 437540C8698F3CFA 70998B4B05D6775E53C55F9CB49FC019
4FB05E1515AB73A7 072D43A077075292 FA83CA46DBE5B94B7A8E7BFA937E89A3
49E95D6D4CA229BF 02FE55778117F12A 13AB912B7A336622CF9C5D7A4986ADB5
018310DC409B26D6 1D9D5C5018F728C2 AFB49C2BF65BB7D0D1ABB290658BC778
1C587F1C13924FEF 305532286D6F295A 96102419A06E717555CB3774D13EF201
0101010101010101 0123456789ABCDEF 40FAC9D4147B6636FA34EC4847B268B2
1F1F1F1F0E0E0E0E 0123456789ABCDEF AECAE1BBF878E283A790795108EA3CAE
E0FEE0FEF1FEF1FE 0123456789ABCDEF 5403D695B6B0CFE6C39E072D9FAC631D
0000000000000000 FFFFFFFFFFFFFFFF A5D0D080D945C4E4014933E0CDAFF6E4
FFFFFFFFFFFFFFFF 0000000000000000 441D9AF888D9E2BAF21E9A77B71C49BC
0123456789ABCDEF 0000000000000000 9EAEB9A14496B5BF245946885754369A
FEDCBA9876543210 FFFFFFFFFFFFFFFF 64609F170CD672BF6B5C5A9C5D9E0A5A
*/


	std::cout << "Testing char:\n" ;
	test_char_block< char          >() ;

	std::cout << "Testing unsigned char:\n" ;
	test_char_block< unsigned char >() ;





/*
	//set_key test data
	char* szKey[] =
	{
		"F0", "F0E1", "F0E1D2", "F0E1D2C3", "F0E1D2C3B4", "F0E1D2C3B4A5",
		"F0E1D2C3B4A596", "F0E1D2C3B4A59687", "F0E1D2C3B4A5968778", "F0E1D2C3B4A596877869",
		"F0E1D2C3B4A5968778695A", "F0E1D2C3B4A5968778695A4B", "F0E1D2C3B4A5968778695A4B3C",
		"F0E1D2C3B4A5968778695A4B3C2D", "F0E1D2C3B4A5968778695A4B3C2D1E", "F0E1D2C3B4A5968778695A4B3C2D1E0F",
		"F0E1D2C3B4A5968778695A4B3C2D1E0F00", "F0E1D2C3B4A5968778695A4B3C2D1E0F0011",
		"F0E1D2C3B4A5968778695A4B3C2D1E0F001122", "F0E1D2C3B4A5968778695A4B3C2D1E0F00112233",
		"F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344", "F0E1D2C3B4A5968778695A4B3C2D1E0F001122334455",
		"F0E1D2C3B4A5968778695A4B3C2D1E0F00112233445566", "F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344556677"
	};
	unsigned char aucKey[24];
	unsigned char aucPlainText[8];
	char szHex[49];
	szHex[16] = 0;
	unsigned char aucCipherText[8];
	ofstream out("out.txt", ios::trunc);
	int iKey;
	for(int i=0; i<24; i++)
	{
		iKey = i+1;
		strcpy(szHex, szKey[i]);
		out << szHex << " ";
		HexStr2CharStr(szHex, aucKey, iKey);
		strcpy(szHex, "FEDCBA9876543210");
		out << szHex << " ";
		HexStr2CharStr(szHex, aucPlainText, 8);
		CBlowFish oBlowFish(aucKey, iKey);
		oBlowFish.Encrypt(aucPlainText, aucCipherText, 8);
		CharStr2HexStr(aucCipherText, szHex, 8);
		out << szHex << endl;
	}
	out.close();
*/

//OK, the same results
/*
Key                                                 Text             Cipher
F0													FEDCBA9876543210 F9AD597C49DB005E
F0E1												FEDCBA9876543210 E91D21C1D961A6D6
F0E1D2												FEDCBA9876543210 E9C2B70A1BC65CF3
F0E1D2C3											FEDCBA9876543210 BE1E639408640F05
F0E1D2C3B4											FEDCBA9876543210 B39E44481BDB1E6E
F0E1D2C3B4A5										FEDCBA9876543210 9457AA83B1928C0D
F0E1D2C3B4A596										FEDCBA9876543210 8BB77032F960629D
F0E1D2C3B4A59687 									FEDCBA9876543210 E87A244E2CC85E82
F0E1D2C3B4A5968778									FEDCBA9876543210 15750E7A4F4EC577
F0E1D2C3B4A596877869								FEDCBA9876543210 122BA70B3AB64AE0
F0E1D2C3B4A5968778695A								FEDCBA9876543210 3A833C9AFFC537F6
F0E1D2C3B4A5968778695A4B							FEDCBA9876543210 9409DA87A90F6BF2
F0E1D2C3B4A5968778695A4B3C							FEDCBA9876543210 884F80625060B8B4
F0E1D2C3B4A5968778695A4B3C2D						FEDCBA9876543210 1F85031C19E11968
F0E1D2C3B4A5968778695A4B3C2D1E						FEDCBA9876543210 79D9373A714CA34F
F0E1D2C3B4A5968778695A4B3C2D1E0F					FEDCBA9876543210 93142887EE3BE15C
F0E1D2C3B4A5968778695A4B3C2D1E0F00					FEDCBA9876543210 03429E838CE2D14B
F0E1D2C3B4A5968778695A4B3C2D1E0F0011				FEDCBA9876543210 A4299E27469FF67B
F0E1D2C3B4A5968778695A4B3C2D1E0F001122				FEDCBA9876543210 AFD5AED1C1BC96A8
F0E1D2C3B4A5968778695A4B3C2D1E0F00112233			FEDCBA9876543210 10851C0E3858DA9F
F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344			FEDCBA9876543210 E6F51ED79B9DB21F
F0E1D2C3B4A5968778695A4B3C2D1E0F001122334455 		FEDCBA9876543210 64A6E14AFD36B46F
F0E1D2C3B4A5968778695A4B3C2D1E0F00112233445566		FEDCBA9876543210 80C7D7D45A5479AD
F0E1D2C3B4A5968778695A4B3C2D1E0F0011223344556677	FEDCBA9876543210 05044B62FA52D080
*/

}

